﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;

public partial class DangKy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadDuLieuNgayThangNam();
        GioiTinhNam.Checked = true;
    }
    public void LoadDuLieuNgayThangNam()
    {
        for (int i = 0; i < 31; i++)
        {
            ddlNgay.Items.Add((i + 1).ToString());
        }

        for (int i = 0; i < 12; i++)
        {
            ddlThang.Items.Add((i + 1).ToString());
        }

        for (int i = 1940; i < 2000; i++)
        {
            ddlNam.Items.Add((i + 1).ToString());
        }
    }

    protected void OK_Click(object sender, EventArgs e)
    {
        if (ThanhVienBUS.KiemTraUserNameDaTonTai(txtTenDanhNhap.Text))
        {
            txtMatKhau.Text = null;
            txtNhacLaiMatKhau.Text = null;
            lbThongBaoLoiTenDangNhap.Visible = true;
            vcNhacLaiMatKhau.IsValid = true;
            return;
        }

        lbThongBaoLoiTenDangNhap.Visible = false;

        //Capcha nhập không đúng
        CapchaSecurity.ValidateCaptcha(txtXacNhanMatMa.Text);
        if (!CapchaSecurity.UserValidated)
        {

            FailCode.IsValid = false;
            vcNhacLaiMatKhau.IsValid = true;
            return;
        }


        ThanhVienDTO thanhvien = new ThanhVienDTO();

        thanhvien.AnhDaiDien = "null";
        thanhvien.DiaChi = txtDiaChi.Text;
        thanhvien.Email = txtEmail.Text;
        if (GioiTinhNam.Checked == true)
            thanhvien.GioiTinh = true;
        else
            thanhvien.GioiTinh = false;
        thanhvien.ID = ThanhVienBUS.DemSoLuongThanhVien() + 1;
        thanhvien.MaNhomThanhVien = 1;
        DateTime ngaysinh = new DateTime(int.Parse(ddlNam.Text), int.Parse(ddlThang.Text), int.Parse(ddlNgay.Text));
        thanhvien.NgaySinh = ngaysinh;
        thanhvien.Password = txtMatKhau.Text;
        thanhvien.SDT = txtSoDienThoai.Text;
        thanhvien.SoCMND = txtCMND.Text;
        thanhvien.SoTienTrongTaiKhoan = 0;
        thanhvien.TenThanhVien = txtHoTen.Text;
        thanhvien.TinhTrangThanhVien = true;
        thanhvien.Username = txtTenDanhNhap.Text;

        if (ThanhVienBUS.ThemThanhVien(thanhvien))
        {
            Response.Redirect("Default.aspx");
        }

    }
}
